package cn.hongxin.sap.conn.jdbc;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class JdbcLinkConfig {

    //创建两个数据源
    /*@Bean(name="sapInterfaceDataSource")
    @Qualifier("sapInterfaceDataSource")
    @ConfigurationProperties(prefix="spring.datasource.sapinterface")
    public DataSource sapInterfaceDataSource(){
        return DataSourceBuilder.create().build();
    }*/

    @Bean(name="sapConfigDataSource")
    @Qualifier("sapConfigDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.sapconfig")
    public DataSource sapConfigDataSource() {
        return DataSourceBuilder.create().build();
    }


    //根据数据源创建jdbcTemplate

    /*@Bean(name="sapInterfaceJdbcTemplate")
    public JdbcTemplate sapInterfaceJdbcTemplate(@Qualifier("sapInterfaceDataSource")DataSource datasource){
        return new JdbcTemplate(datasource);
    }*/

    @Bean(name="sapConfigJdbcTemplate")
    public JdbcTemplate sapConfigJdbcTemplate(@Qualifier("sapConfigDataSource") DataSource datasource){
        return new JdbcTemplate(datasource);
    }
}
